﻿#include "TestTask.h"
#include <QDebug>
#include <QThread>
#include <QTimer>
#include <QCoreApplication>
#include <QTest>

TestTask::TestTask(QObject *parent) : ZMultiThreadTask(parent)
{
    connect(this, SIGNAL(sigTaskFinished()), this, SLOT(slotTaskFinished()));
    m_main_thread_id = QThread::currentThreadId();
}

int TestTask::type()
{
    return 0;
}

void TestTask::stopCurrent()
{

}

void TestTask::doTask()
{
    for (int i = 0; i < 10; i++)
    {
        _sleep(100);
        qDebug() << "do task in thread" << QThread::currentThreadId();
    }

    // 保证在多线程中执行
    QVERIFY(m_main_thread_id != QThread::currentThreadId());

    // 发送任务结束信号
    emit sigTaskFinished();
}

void TestTask::slotTaskFinished()
{
    qApp->quit();
    qDebug() << "Task finished, Current thread" << QThread::currentThreadId();
}
